sortitems = 0;

function move(fbox,tbox)
{
   var j;
   for(var i=0; i<fbox.options.length; i++)
   {
      if(fbox.options[i].selected && fbox.options[i].value != "") {
      	var no = new Option();
      	no.value = fbox.options[i].value;
      	no.text = fbox.options[i].text;
      	tbox.options[tbox.options.length] = no;
      	fbox.options[i].selected = false;
      	fbox.options[i].value = "";
      	fbox.options[i].text = "";
      	j=i;
   	  }
   }
   BumpUp(fbox,j);
   if (sortitems) 
   		SortD(tbox);
	 setMemberID(document.formIon.roles2);
}

function movebox(fbox,tbox,idarray)
{
   var j;
   for(var i=0; i<fbox.options.length; i++)
   {
      if(fbox.options[i].value==0) {
      	var no = new Option();
      	no.value = fbox.options[i].value;
      	no.text = fbox.options[i].text;
      	tbox.options[tbox.options.length] = no;
      	fbox.options[i].selected = false;
      	fbox.options[i].value = "";
      	fbox.options[i].text = "";
      	j=i;
   	  }
   }
   BumpUp(fbox,j);	
}

function moveUp(box)
{
   for(var i=0; i<box.options.length; i++)
   {
      if(box.options[i].selected && box.options[i].value != "" && i!=0) {      		
      	var no = new Option();
      	no.value = box.options[i].value;
      	no.text = box.options[i].text;      	
      	box.options[i].value = box.options[i-1].value;
      	box.options[i].text = box.options[i-1].text;
      	box.options[i].selected = false;      	
      	box.options[i-1].value = no.value;
      	box.options[i-1].text = no.text;      	
      	box.options[i-1].selected = true;      	
   	  }
   }
}

function moveDown(box)
{
   for(var i=box.options.length-1; i>=0; i--)
   {
      if(box.options[i].selected && box.options[i].value != "" && i!=box.options.length-1) {     		
      	var no = new Option();
      	no.value = box.options[i].value;
      	no.text = box.options[i].text;      	
      	box.options[i].value = box.options[i+1].value;
      	box.options[i].text = box.options[i+1].text;
      	box.options[i].selected = false;      	
      	box.options[i+1].value = no.value;
      	box.options[i+1].text = no.text;      	
      	box.options[i+1].selected = true;      	
   	  }
   }
}

function BumpUp(box,k)  {
   var num=0;
   for(var i=box.options.length-1; i>=0; i--)
   {
      if(box.options[i].value == "") {
      	for(var j=i; j<box.options.length-1; j++)
      	{
        	box.options[j].value = box.options[j+1].value;
          box.options[j].text = box.options[j+1].text;
        }
      	box.options.length=box.options.length-1;
   	  }
   }
   if(k<box.options.length)
      box.options[k].selected = true;
   else {
      if(box.options.length>0)
      {
         k=box.options.length-1;
         box.options[k].selected = true;
      }
   }
}

function SortD(box)  {
   var temp_opts = new Array();
   var temp = new Object();
   for(var i=0; i<box.options.length; i++)  {
      temp_opts[i] = box.options[i];
   }
   for(var x=0; x<temp_opts.length; x++)  {
      for(var y=(x+1); y<temp_opts.length; y++)  {
         if(temp_opts[x].value > temp_opts[y].value)  {
            temp = temp_opts[x].text;
            temp_opts[x].text = temp_opts[y].text;
            temp_opts[y].text = temp;
            temp = temp_opts[x].value;
            temp_opts[x].value = temp_opts[y].value;
            temp_opts[y].value = temp;
         }
      }
   }
   for(var i=0; i<box.options.length; i++)  {
      box.options[i].value = temp_opts[i].value;
      box.options[i].text = temp_opts[i].text;
   }
}

function setMemberID(box){
   var memberid="";
   for(var i=0; i<box.options.length; i++)  {
      if(box.options[i].value!="")
         memberid=memberid + box.options[i].value + ",";
   }
   document.formIon.memberid.value=memberid;
}